package com.run.go.gearbox.mysql.codec.parse;

import com.run.go.gearbox.mysql.enums.ClientCapabilities;
import com.run.go.gearbox.mysql.enums.MysqlCharacterSet;
import com.run.go.gearbox.mysql.enums.ServerStatus;
import com.run.go.gearbox.mysql.io.BoundedInputStream;
import com.run.go.gearbox.mysql.io.IoUtils;
import com.run.go.gearbox.mysql.response.ErrorResponse;
import com.run.go.gearbox.mysql.response.OkResponse;
import com.run.go.gearbox.mysql.response.ServerGreeting;
import com.run.go.gearbox.mysql.response.ServerPacket;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.EOFException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Set;


public class Connected extends ResponseStart {
	
	protected final static Logger log = LoggerFactory.getLogger(Connected.class);
	
    /**
     * The salt size in a server greeting
     */
    public static final int SALT_SIZE = 8;

    /**
     * The size of the second salt in a server greeting
     */
    public static final int SALT2_SIZE = 12;

    /**
     * Number of unused bytes in server greeting
     */
    public static final int GREETING_UNUSED_SIZE = 13;



    @Override
    public ServerPacket parse(int length,
                              int packetNumber,
                              BoundedInputStream in,
                              Channel channel) throws IOException {
        ServerPacket serverPacket =  super.parse(length,packetNumber,in,channel);
        if(serverPacket instanceof OkResponse.RegularOK){
            OkResponse.RegularOK regularOK = (OkResponse.RegularOK) serverPacket;
            OkResponse.FinishLogin finishLogin = new OkResponse.FinishLogin(regularOK.getPacketLength()
                    ,regularOK.getPacketNumber(),regularOK.getAffectedRows(),regularOK.getInsertId()
                    ,regularOK.getServerStatus(),regularOK.getWarningCount(),regularOK.getMessage());
            return finishLogin;
        }
        return serverPacket;
    }


}
